#include <QFile>
#include <QTextStream>
#include <QTime>
#include <QtDebug>
#include <QThread>
#include "file_data_read_processing.h"

#include "../data/constant_variables.h"
#include "../data/track_id_data_integrated.h"

File_data_read_processing::File_data_read_processing(QObject *parent) :
    QObject(parent)
{
    init_connections();
}

File_data_read_processing::~File_data_read_processing()
{

}

void File_data_read_processing::init_connections()
{
    connect(this,
            static_cast<void (File_data_read_processing::*)(QStringList)>(&File_data_read_processing::sig_open_file),
            this,
            static_cast<void (File_data_read_processing::*)(QStringList)>(&File_data_read_processing::read_data_and_send_data_to_model));

}

void File_data_read_processing::read_data_and_send_data_to_model(QStringList files_name_with_path)
{

//    qDebug()<< "File_data_read_processing thread: " << (quint64) QThread::currentThreadId() << Qt::endl;

    for (int index(0); files_name_with_path.count() != index; ++index) {
        QFile file(files_name_with_path.at(index));

        if ( file.open(QIODevice::ReadOnly)) {
            QTextStream out(&file);
            out.setCodec("UTF-8");

            QString file_content("");

            while ( 0 != (file_content = out.readLine()).length()) {
                Track_id_data_integrated* data_integrated(new Track_id_data_integrated(file_content));

                emit sig_integrated_data(data_integrated);
            }
        }

        file.close();
    }
}
